module.exports = class ordersService {
  static async getSaleOrdersData(uid, page) {
    const orders = require("../model/orders")();
    return await orders
      .where({ uid })
      .order("listid", "DESC")
      .limit((page - 1) * 10, page * 10)
      .select();
  }

  static async getBuyOrdersData(buyid, page) {
    const orders = require("../model/orders")();
    return await orders
      .where({ buyid })
      .order("listid", "DESC")
      .limit((page - 1) * 10, page * 10)
      .select();
  }

  static async getSum(id, type) {
    const orders = require("../model/orders")();
    if (type === "sale") {
      return await orders.where({ uid: id }).count();
    } else if (type === "buy") {
      return await orders.where({ buyid: id }).count();
    } else return 0;
  }

  static async addOrders(data) {
    const orders = require("../model/orders")();
    return await orders.insert(data);
  }

  // 删除
  static async batchDeteleOrders(data) {
    const orders = require("../model/orders")();

    const listids = data.map((item) => item.listid);
    for (let i = 0; i < data.length; i++) {
      await orders
        .where({
          listid: listids[i],
        })
        .del();
    }
    return true;
  }
};
